/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.lestr.genver.data.collection.list;

import org.lestr.genver.data.collection.CollectionListener;
import java.util.ArrayList;
import java.util.Collection;

/**
 *
 * @author PIBONNIN
 */
public class ListenedArrayList<E> extends ArrayList<E> implements ListenedList<E> {


    private transient Collection<CollectionListener<E>> listeners;


    public ListenedArrayList() {

        listeners = new ArrayList<CollectionListener<E>>();

    }


    @Override
    public Collection<CollectionListener<E>> getListeners() {

        return listeners;

    }


    @Override
    public boolean add(E e) {

        boolean rslt = super.add(e);

        if (rslt)
            for (CollectionListener<E> listener : listeners)
                listener.elementAdded(this, e);

        return rslt;

    }


    @Override
    public void add(int index,
                    E element) {

        super.add(index, element);

        for (CollectionListener<E> listener : listeners)
            listener.elementAdded(this, element);

    }


    @Override
    public boolean addAll(Collection<? extends E> c) {

        boolean rslt = super.addAll(c);

        if (rslt)
            for (CollectionListener<E> listener : listeners)
                for (E e : c)
                    listener.elementAdded(this, e);

        return rslt;

    }


    @Override
    public boolean addAll(int index,
                          Collection<? extends E> c) {

        boolean rslt = super.addAll(index, c);

        if (rslt)
            for (CollectionListener<E> listener : listeners)
                for (E e : c)
                    listener.elementAdded(this, e);

        return rslt;

    }


    @Override
    public boolean remove(Object o) {

        for (CollectionListener<E> listener : listeners)
            listener.elementRemoving(this, (E) o);

        return super.remove((E) o);

    }


    @Override
    public E remove(int index) {

        for (CollectionListener<E> listener : listeners)
            listener.elementRemoving(this, get(index));

        return super.remove(index);

    }


    @Override
    protected void removeRange(int fromIndex,
                               int toIndex) {

        for (int i = fromIndex; i <= toIndex; i = i + 1)
            for (CollectionListener<E> listener : listeners)
                listener.elementRemoving(this, get(i));

        super.removeRange(fromIndex, toIndex);

    }


    @Override
    public E set(int index,
                 E element) {

        boolean realSet = get(index) != element;

        if (realSet)
            for (CollectionListener<E> listener : listeners)
                listener.elementRemoving(this, get(index));

        E rslt = super.set(index, element);

        if (realSet)
            for (CollectionListener<E> listener : listeners)
                listener.elementAdded(this, get(index));

        return rslt;

    }


    @Override
    public boolean removeAll(Collection<?> c) {

        for (CollectionListener<E> listener : listeners)
            for (Object o : c)
                listener.elementRemoving(this, (E) o);

        return super.removeAll(c);

    }


    @Override
    public void clear() {

        for (CollectionListener<E> listener : listeners)
            for (Object o : this)
                listener.elementRemoving(this, (E) o);

        super.clear();

    }


}

